#ifndef EDITORVIEW_H
#define EDITORVIEW_H

#include<QGraphicsView>
#include<QGraphicsItem>
#include <QWidget>
#include<QtGui>
#include<QGraphicsScene>
#include<QRectF>
#include<QDebug>
#include<QMouseEvent>
#include<QKeyEvent>
#include<typeinfo>      //qDebug()<<typeid(obj).name();

#include<QDataStream>


#include"editor_scene.h"
#include"node_scene.h"
#include"item_enum.h"
#include"node_edge.h"
#include"node_temp_edge.h"

enum EDGE_MODE{
    EDGE_NOOP = 0,
    EDGE_DRAG = 1,
    EDGE_CUTE = 2

};

class EditorView : public QGraphicsView
{
public:
    EditorView(EditorScene *scene,QWidget *parent=0);
    ~EditorView();


    void mousePressEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    void wheelEvent(QWheelEvent *event) override;  // 鼠标滑轮影响场景大小
    void mouseMoveEvent(QMouseEvent *event) override;
    void keyPressEvent(QKeyEvent *event) override;

    void middleMouseButtonPress(QMouseEvent *event);
    void middleMouseButtonRelease(QMouseEvent *event);
    void leftMouseButtonPress(QMouseEvent *event);
    void RightMouseButtonPress(QMouseEvent *event);
    void LeftMouseButtonRelease(QMouseEvent *event);
    void RightMouseButtonRelease(QMouseEvent *event);

    void QuerySocket(QPointF pos);
    void SetNodeTitle(int index, QString title);

    void addNodeItem(int index);
    void addNodeItem(int index,int type);
    void AddEdge(int start_node_index,int start_socket_index,int end_node_index,int end_socket_index);

    NodeScene* DeleteNode(QPointF pos);
    NodeEdge* DeleteEdge(int node_input_index,int socket_input_index,int node_output_index,int socket_output_index);
    void DeleteNode(NodeScene node);
    void RemoveEdge(int start_node_index,int start_socket_index,int end_node_index,int end_socket_index);

    QGraphicsItem* NodeToItem(NodeScene *node);
    QGraphicsItem* EdgeToItem(NodeEdge *edge);

    void UpdateNodeData();
    void QueryNodeDate();


public:
//    friend QDataStream &operator<<(QDataStream &,const EditorView &);
//    friend QDataStream &operator>>(QDataStream &,EditorView &);

public:
    //EditorScene *dataScene;
    EditorScene *editorScene; //绘制背景
    QVector<NodeScene*> node;
    QVector<NodeEdge*> edges;
    NodeEdge *edge;
    NodeTempEdge *temp_edge;

    bool is_temp_alive = false;
    int number = 100;

    QGraphicsItem *obj;
    QGraphicsItem *item;

    bool output_ready = false;
    bool input_ready = false;
    int input_node_index;
    int input_socket_index;
    int output_node_index;
    int output_socket_index;





private:
    // 缩放参数
    float zoomInFactor = 1.25;
    int zoom = 0;
    int zoomStep = 1;
    int zoomRange[2] = {-8,3};
    BOOLEAN zoomClamp = true;
};

#endif // EDITORVIEW_H
